﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->customPlotWidget->init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_comBtn_clicked()
{

}

void MainWindow::on_showAllBtn_clicked()
{
    ui->customPlotWidget->showAllGraph();
}

void MainWindow::on_testBtn_clicked()
{
    ui->customPlotWidget->demoTest();
}

void MainWindow::on_clearGraphBtn_clicked()
{
    ui->customPlotWidget->clear();
}

/*
 * Excel表格的列数是用26进制A1、B2、C3、D4….Z26、AA27、AB28、AC29..表示的，为程序处理方便，第29列可表示为AC
 */
QString MainWindow:: ConvertSheetColumnFromNumber(int number)
{
    QString resultStr = "";
    while(number > 0)
    {
        int k = number % 26;
        if(k == 0)
            k = 26;
        resultStr.push_front(QChar(k + 64));
        number = (number - k) / 26;
    }

    return resultStr;
}

void MainWindow::on_fileBtn_clicked()
{
/*
 *  QString getOpenFileName(QWidget *parent = Q_NULLPTR,
                                   const QString &caption = QString(),
                                   const QString &dir = QString(),
                                   const QString &filter = QString(),
                                   QString *selectedFilter = Q_NULLPTR,
                                   Options options = Options());
 *  parent，用于指定父组件。
 *  caption，是对话框的标题；
 *  dir，是对话框显示时默认打开的目录，
 *      "." 代表程序运行目录，
 *      "/" 代表当前盘符的根目录(Windows，Linux下/就是根目录了)，比如"C:\\"等；
 *      打开程序运行目录下的Data文件夹作为默认打开路径，应该写成"./Data/"
 *      若想有一个默认选中的文件，则在目录后添加文件名即可："./Data/teaser.graph"
 *  filter，是对话框的后缀名过滤器，"Image Files(*.jpg *.png)"只后缀名是jpg或者png的文件。
 *      需要使用多个过滤器，使用";;"分割，"JPEG Files(*.jpg);;PNG Files(*.png)"
 *  selectedFilter，是默认选择的过滤器；
 *  options，对话框的一些参数设定，如只显示文件夹等，它的取值是enum QFileDialog::Option，每个选项可以使用 | 运算组合起来
 */
    QFile qfile;
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    u8"选择文件",
                                                    "/",
                                                    "txt(*.txt);;excel(*.xls *.xlsx);;all(*.*)");

    int qlen = fileName.length();
    if(qlen < 5) return;

    qfile.setFileName(fileName);
    QString suffixName = fileName.mid(qlen - 3, 3);
    QString suffixName1 = fileName.mid(qlen - 4, 4);
    suffixName  = suffixName.toLower();
    suffixName1 = suffixName1.toLower();
    if(suffixName == "txt") {
        if(qfile.open(QIODevice::ReadWrite)) {
            ui->fileLineEdit->setText(fileName);

            QTextEdit txtEdit;
            txtEdit.append("record.at(i)");

            QTextStream stream(&qfile);
            stream << txtEdit.document()->toPlainText();

            qfile.close();
        }
    }
    else if(suffixName == "xls" || suffixName1 == "xlsx") {
        QAxObject *pExcel = new QAxObject(); //new QAxObject("Excel.Application");
        if(!pExcel->setControl("ket.Application")) {
            qDebug() << "PC not support WPS, try office";
            if(!pExcel->setControl("Excel.Application")) {
                QMessageBox::information(nullptr, "warning", "PC not supported office!");
                return;
            }
        }
        //操作文件时可见
        //pExcel->dynamicCall("SetVisible(bool)", true);
        ui->fileLineEdit->setText(fileName);

        QAxObject *workBooks, *workbook;
        workBooks = pExcel->querySubObject("WorkBooks");
        workbook  = workBooks->querySubObject("Open(QString&)", fileName);

        QAxObject *workSheets = workbook->querySubObject("WorkSheets");
        int workSheetNum = workSheets->property("Count").toInt();

        QAxObject *workSheet1 = workSheets->querySubObject("Item(int)", 1);
        QAxObject *rangeWorkSheet1 = workSheet1->querySubObject("UsedRange");

        QAxObject *rowsSheet1 = rangeWorkSheet1->querySubObject("Rows");
        int rowsSheetCnt = rowsSheet1->property("Count").toInt();

        QAxObject *columnsSheet1 = rangeWorkSheet1->querySubObject("Columns");
        int columnsSheetCnt = columnsSheet1->property("Count").toInt();

        int rowsStart = rowsSheet1->property("Row").toInt();
        int columnStart = columnsSheet1->property("Column").toInt();

        if(columnsSheetCnt > 3) columnsSheetCnt = 3;

        for(int i = rowsStart, j = columnStart; i < rowsSheetCnt; i++) {

        }

        workbook->dynamicCall("Close()");
        workbook->dynamicCall("Quit()");
        delete pExcel;
        pExcel = nullptr;
    }
    else {
        QMessageBox::information(nullptr, "warning", "Invalid files");
    }
}

void MainWindow::on_fileDrawBtn_clicked()
{

}

void MainWindow::on_fileSimulateBtn_clicked()
{

}
